package com.danco.training.zaneuskaya.repository.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.danco.training.zaneuskaya.domain.Forum;
import com.danco.training.zaneuskaya.domain.Note;
import com.danco.training.zaneuskaya.domain.User;
import com.danco.training.zaneuskaya.repository.NoteDao;
/**
 * @author Zaneuskaya Aliaksandra
 *
 */
@Repository
public class NoteDaoImpl extends AbstractHibernateDao<Note, Integer> implements NoteDao {

	
	private static final String FORUM_NOTE = "forum";
	private static final String NOTE_TIME = "noteTime";
	private static final String NOTE_SENDER = "noteSender";

	@Override
	public Note read(Integer PK) throws HibernateException {
		return super.read(Note.class, PK);
	}

	@Override
	public List<Note> readAll() throws HibernateException {

		return super.readAll(Note.class, NOTE_TIME);
	}

	
	@SuppressWarnings("unchecked")
	@Override
	public List<Note> findNotesBySender(User user) throws HibernateException{
		List<Note> notes = new ArrayList<Note>();
		Criteria criteria = getSession().createCriteria(Note.class);
		criteria.add(Restrictions.eq(NOTE_SENDER, user));
		criteria.addOrder(Order.desc(NOTE_TIME));
		notes =  criteria.list();
		return notes;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Note> findNotesByPeriod(Date from, Date to)throws HibernateException {
		List<Note> notes = new ArrayList<Note>();
		Criteria criteria = getSession().createCriteria(Note.class);
		criteria.add(Restrictions.between(NOTE_TIME, from,to));
		
		notes =  modifyCriteria(criteria,FORUM_NOTE).list();
		return notes;
	}

	@SuppressWarnings("unchecked")
	public List<Note> sortNotesByTimeColumn(String column) throws HibernateException{
		List<Note> notes = new ArrayList<Note>();
		Criteria criteria = getSession().createCriteria(Note.class);
		criteria.addOrder(Order.desc(column));
		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		notes = criteria.list();
		return notes;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Note> findNotesByForum(Forum forum) throws HibernateException{
		List<Note> notes = new ArrayList<Note>();
		Criteria criteria = getSession().createCriteria(Note.class);
		criteria.add(Restrictions.eq(FORUM_NOTE, forum));
		
		notes = modifyCriteria(criteria,NOTE_TIME).list();
		return notes;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Note> findNotesByPeriodUserForum(Date from, Date to, User user,
			Forum forum) throws HibernateException{
		List<Note> notes = new ArrayList<Note>();
		Criteria criteria = getSession().createCriteria(Note.class);
		criteria.add(Restrictions.eq(NOTE_SENDER, user));
		criteria.add(Restrictions.between(NOTE_TIME, from,to));
		criteria.add(Restrictions.eq(FORUM_NOTE, forum));
		notes = modifyCriteria(criteria,NOTE_TIME).list();
		return notes;
	}

	

}
